layui.use(['form','layer','jquery'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer
        $ = layui.jquery;

    //表单输入效果
    $(".loginBody .input-item").click(function(e){
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    })
    $(".loginBody .layui-form-item .layui-input").focus(function(){
        $(this).parent().addClass("layui-input-focus");
    })
    $(".loginBody .layui-form-item .layui-input").blur(function(){
        $(this).parent().removeClass("layui-input-focus");
        if($(this).val() != ''){
            $(this).parent().addClass("layui-input-active");
        }else{
            $(this).parent().removeClass("layui-input-active");
        }
    })
})

function login(){
    //登录按钮
    var userName = $("#userName").val();
    var password = $("#password").val();
    var captcha = $("#code").val();
    var rememberMe = $("#rememberMe").is(":checked");
    if (!userName) {
        top.layer.msg("请输入用户名");
        return;
    }
    if (!password) {
        top.layer.msg("请输入密码！");
        return;
    }
    if (!captcha || captcha.length != 4) {
        top.layer.msg("请输入4位验证码！");
        return;
    }
    $("#loginChange").val("登录中...");
    $.post("login",{
        userName : userName,
        password : password,
        captcha : captcha,
        rememberMe : rememberMe,
    },function(res){
        if (res.success){
            top.layer.msg(res.message);
            $("#loginChange").val("跳转中...");
            setTimeout(function(){
                window.location.href = "center"
            },500);
        }else{
            top.layer.msg(res.message);
            setTimeout(function(){
                Load();
                $("#loginChange").val("重新登录");
            },500);
        }
    })
}

//验证码局部刷新
function Load() {
    $(".loginRefresh").load(location.href + " .loginRefresh");
}

